package com.example.understandvideo.tool;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.time.LocalDate;
import java.util.Random;
import java.util.UUID;

public class Tools {

    public final static String getUUID() {
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV < 0) {//有可能是负数
            hashCodeV = -hashCodeV;
        }
        String uuid = String.format("%016d", hashCodeV);
        return uuid;
    }

    public static String readImage(String path) {
        byte[] fileByte = null;
        String code =null;
        try {
            File file = new File(path);
            fileByte = Files.readAllBytes(file.toPath());
            String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
            code = "data:image/" + suffix + ";base64," + Base64.encodeBase64String(fileByte);
            //System.out.println(code);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return code;
    }

    public static String readImageV2(String path) {
        byte[] fileByte = null;
        String code =null;
        try {
            File file = new File(path);
            fileByte = Files.readAllBytes(file.toPath());
            String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
            code = Base64.encodeBase64String(fileByte);
            //System.out.println(code);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return code;
    }


}
